using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace COME342.CollisionResolution.BinaryTree
{
    class Program
    {
        static void WaitKey()
        {
            Console.WriteLine();
            Console.WriteLine("Press 'c' to continue...");
            Console.WriteLine();
            char x;
            while (Console.ReadKey(true).KeyChar != 'c') ;
            Console.Clear();                
        }

        static void Main(string[] args)
        {

            Console.WriteLine("- Binary Search Tree -");
            BinarySearchTree<string> bst = new BinarySearchTree<string>("Dallas");            
            bst.Insert("Oakland");
            bst.Insert("Pittsburgh");
            bst.Insert("Miami");
            bst.Insert("Chicago");
            bst.Insert("Denver");
            bst.Insert("Boston");
            bst.Insert("Spivey's Corner");

            frmBSTTree fbst = new frmBSTTree(bst);            
            fbst.ShowDialog();

            List<string> lsIn = bst.InOrderTraverse();
            List<string> lsPre = bst.PreOrderTraverse();
            List<string> lsPost = bst.PostOrderTraverse();
            Console.WriteLine("In Order".PadRight(20) + "PreOrder".PadRight(20) + "PostOrder".PadRight(20) + "\n");            
            for (int i = 0; i < lsIn.Count; ++i)
            {
                Console.WriteLine(lsIn[i].PadRight(20) + lsPre[i].PadRight(20) + lsPost[i].PadRight(20));
            }

            Console.WriteLine();
            Console.WriteLine("- AVL Tree -");

            AVLTree<string> avl = new AVLTree<string>("Dallas");            
            avl.Insert("Oakland");
            avl.Insert("Pittsburgh");
            avl.Insert("Miami");
            avl.Insert("Chicago");
            avl.Insert("Denver");
            avl.Insert("Boston");
            avl.Insert("Spivey's Corner");

            frmAVLTree ft = new frmAVLTree(avl);            
            ft.ShowDialog();

            lsIn = avl.InOrderTraverse();
            lsPre = avl.PreOrderTraverse();
            lsPost = avl.PostOrderTraverse();
            Console.WriteLine("In Order".PadRight(20) + "PreOrder".PadRight(20) + "PostOrder".PadRight(20) + "\n");            
            for (int i = 0; i < lsIn.Count; ++i)
            {
                Console.WriteLine(lsIn[i].PadRight(20) + lsPre[i].PadRight(20) + lsPost[i].PadRight(20));
            }


            WaitKey();

            
            SimpleDataRow<string> sdr;
            PseudoLinkRow<string> plr;
            BinaryTreeLisch<SimpleDataRow<string>> bti = new BinaryTreeLisch<SimpleDataRow<string>>(11);
            LISCH<SimpleDataRow<string>> lisch = new LISCH<SimpleDataRow<string>>(11);
            ComputedChaining<PseudoLinkRow<string>> cc = new ComputedChaining<PseudoLinkRow<string>>(11);
            
            List<SimpleDataRow<string>> sdrList = new List<SimpleDataRow<string>>();

            StreamReader sr = new StreamReader(@"come342.ker");
            string s;
            Console.WriteLine("- Input File for Collision Resolvers -");
            while (!sr.EndOfStream)
            {
                s = sr.ReadLine();
                Console.WriteLine(s.Replace(';', '\t'));
                sdr = new SimpleDataRow<string>(int.Parse(s.Split(new char[] { ';' })[0]), s.Split(new char[] { ';' })[1]);
                plr = new PseudoLinkRow<string>(int.Parse(s.Split(new char[] { ';' })[0]), s.Split(new char[] { ';' })[1]);                
                bti.Insert(sdr);
                lisch.Insert(sdr);
                sdrList.Add(sdr);
            }

            WaitKey();

            Console.WriteLine("LISCH");
            Console.WriteLine("No ID\tLink\tData");
            Console.WriteLine(lisch);
            Console.WriteLine("Average Probes in LISCH: " + lisch.GetAverageProbes().ToString("N2"));

            WaitKey();   

            Console.WriteLine("Binary Tree Collision Resolution");
            Console.WriteLine(bti);

            WaitKey();

            Console.WriteLine("Computed Chaining is incomplete.");

            WaitKey();

            //delete every third record
            lisch.DeleteRecord(sdrList[0]);
            lisch.DeleteRecord(sdrList[3]);
            lisch.DeleteRecord(sdrList[6]);
            Console.WriteLine("LISCH after deletions");
            Console.WriteLine(lisch);

            WaitKey();

            //reinsert first half
            lisch.Insert(sdrList[0]);
            Console.WriteLine("LISCH after reinsertion");
            Console.WriteLine(lisch);
            Console.WriteLine("Average Probes again: " + lisch.GetAverageProbes().ToString("N2"));

            WaitKey();
            
        }
    }
}
